﻿using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;
using SpecFlowAlias = TechTalk.SpecFlow;
using Liekhus.Testing.BDD.Core;
using TechTalk.SpecFlow;

namespace Liekhus.Testing.BDD.Web
{
    //[Binding]
    public class StepDefinition : BaseStepDefinitions
    {
        private void OpenPage(string page)
        {
            string url = string.Empty;
            if (ConfigurationManager.AppSettings[string.Format("Page.{0}", page)] != null)
            {
                url = ConfigurationManager.AppSettings[string.Format("Page.{0}", page)].ToString();
            }

            if (string.IsNullOrEmpty(url) == false)
            {
                WebBrowser.Current.GoTo(string.Format("{0}{1}", ConfigurationManager.AppSettings["Site.Url"].ToString(), url));
            }
        }

        public override void GivenIAmLoggedInAs(string username)
        {
            WhenIAmAtTheScreen("Login");
            SpecFlowAlias.Table table = new SpecFlowAlias.Table("Field", "Value");
            table.AddRow("UserName", ConfigurationManager.AppSettings[string.Format("Login.{0}.Username", username)]);
            table.AddRow("Password", ConfigurationManager.AppSettings[string.Format("Login.{0}.Password", username)]);
            WhenIEnterTheFollowingInformation(table);
            WhenIClickTheButton("Log On");
        }

        [Given("I close the browser")]
        public void GivenICloseTheBrowser()
        {
            WebBrowser.Current.Close();
        }

        //[Given("I am at the \"(.*)\" screen")]
        public override void WhenIAmAtTheScreen(string page)
        {
            OpenPage(page);
        }

        //[When("I click the \"(.*)\" link")]
        public override void WhenIClickTheLink(string text)
        {
            var link = WebBrowser.Current.Link(Find.ByText(text));

            if (link.Exists == false)
            {
                Assert.Fail(string.Format("Could not find {0} link on the page.", text));
            }
            link.Click();
        }

        //[When("I click the \"(.*)\" button")]
        public override void WhenIClickTheButton(string text)
        {
            var button = WebBrowser.Current.Button(Find.ByValue(text));

            if (button.Exists == false)
            {
                Assert.Fail(string.Format("Could not find {0} button on the page.", text));
            }
            button.Click();
        }

        //[When("I enter the following information")]
        public override void WhenIEnterTheFollowingInformation(SpecFlowAlias.Table table)
        {
            foreach (var row in table.Rows)
            {
                bool isValid = false;
                isValid = FillTextBox(row["Field"], row["Value"]);
                if (isValid == false) { isValid = FillSelectList(row["Field"], row["Value"]); }

                if (isValid == false)
                {
                    Assert.Fail(string.Format("Field {0} could not be found on the page.", row["Field"]));
                }
            }
        }

        private bool FillTextBox(string name, string value)
        {
            bool isValid = false;

            TextField field = WebBrowser.Current.TextField(Find.ByName(name));
            isValid = field.Exists;
            if (isValid)
            {
                field.TypeText(value);
            }
            return isValid;
        }

        private bool FillSelectList(string name, string value)
        {
            bool isValid = false;

            SelectList field = WebBrowser.Current.SelectList(Find.ByName(name));
            isValid = field.Exists;
            if (isValid)
            {
                field.Select(value);
            }
            return isValid;
        }

        private bool VerifyTextBoxValue(string name, string value)
        {
            bool isValid = false;

            TextField field = WebBrowser.Current.TextField(Find.ByName(name));
            isValid = field.Exists;
            if (isValid)
            {
                isValid = (field.Value == value);
            }
            return isValid;
        }

        private bool VerifySelectListValue(string name, string value)
        {
            bool isValid = false;

            SelectList field = WebBrowser.Current.SelectList(Find.ByName(name));
            isValid = field.Exists;
            if (isValid)
            {
                isValid = (field.SelectedItem == value);
            }
            return isValid;
        }

        //[Then("I should see the following results")]
        public override void ThenIShouldSeeTheFollowingResults(SpecFlowAlias.Table table)
        {
            foreach (var row in table.Rows)
            {
                bool isValid = false;

                isValid = VerifyTextBoxValue(row["Field"], row["Value"]);
                if (isValid == false) { isValid = VerifySelectListValue(row["Field"], row["Value"]); }
                if (isValid == false)
                {
                    Assert.Fail(string.Format("Could not find {0} field on the page.", row["Field"]));
                }
                Assert.IsTrue(isValid == true, string.Format("Field {0} does not contain a value of {1}", row["Field"], row["Value"]));
            }
        }

        //[Then("I should see the following on the screen")]
        public override void ThenIShouldSeeTheFollowingOnScreen(SpecFlowAlias.Table table)
        {
            foreach (var row in table.Rows)
            {
                var value = row["Value"];
                Assert.IsTrue(WebBrowser.Current.ContainsText(value), string.Format("Could not find text '{0}' on the page.", value));
            }
        }

    }
}
